package com.tedu.dao.impl;

import com.tedu.dao.UserDao;
import com.tedu.entry.User;
import com.tedu.entry.vo.UserVO;
import com.tedu.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Author：毕龙
 * Date: 2023/7/28 10:23
 */
public class UserDaoImpl implements UserDao {

    @Override
    public User selectUserByUP(UserVO userVO) {
        Connection conn = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        User user = null;
        try {
            //1.获取链接
            conn = JDBCUtil.getConnection();

            //2.执行sql
            String sql = "select id,username,realname,password,sex from user where username = ? and password = ?";
            pstm = conn.prepareStatement(sql);
            pstm.setString(1,userVO.getUsername());
            pstm.setString(2,userVO.getPassword());
            rs = pstm.executeQuery();

            //3.处理查询结果
            if(rs.next()){
                user = new User();
                user.setId(rs.getInt(1))
                        .setUsername(rs.getString(2))
                        .setRealname(rs.getString(3))
                        .setPassword(rs.getString(4))
                        .setSex(rs.getString(5));

            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {

            //4.释放资源
            JDBCUtil.release(rs,pstm);
            JDBCUtil.release(conn);
        }


        return user;
    }
}
